using System;
using System.Drawing;
using System.Windows.Forms;

namespace QuickCapture
{
	/// <summary>
	/// Description of CaptureScreenForm.
	/// </summary>
	public partial class CaptureScreenForm : Form
	{
		private bool leftButtonDown = false;

        private Point clickPoint = new Point();
        private Point currentTopLeft = new Point();
        private Point currentBottomRight = new Point();

        private Graphics g;
        private Pen myPen = new Pen(Color.Blue, 1);
        private Pen eraserPen = new Pen(Color.DimGray);
        
		public CaptureScreenForm()
		{
			InitializeComponent();
			
			myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            g = this.CreateGraphics();
		}
		
		void CaptureScreenFormMouseDown(object sender, MouseEventArgs e)
		{
			g.Clear(Color.DimGray);
            leftButtonDown = true;
            clickPoint = new Point(System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y);
		}
		
		void CaptureScreenFormMouseUp(object sender, MouseEventArgs e)
		{
			leftButtonDown = false;
            
            Point StartPoint = new Point(currentTopLeft.X, currentTopLeft.Y);
            Rectangle bounds = new Rectangle(currentTopLeft.X, currentTopLeft.Y, currentBottomRight.X - currentTopLeft.X, currentBottomRight.Y - currentTopLeft.Y);
            Screenshot.CaptureImage(StartPoint, Point.Empty, bounds, System.IO.Path.GetTempPath() + "ScreenCapture.jpg");
            
            Close();
		}
		
		void CaptureScreenFormMouseMove(object sender, MouseEventArgs e)
		{
			//Resize (actually delete then re-draw) the rectangle if the left mouse button is held down
            if (leftButtonDown)
            {
                //Erase the previous rectangle
                g.DrawRectangle(eraserPen, currentTopLeft.X, currentTopLeft.Y, currentBottomRight.X - currentTopLeft.X, currentBottomRight.Y - currentTopLeft.Y);

                //Calculate X Coordinates
                if (Cursor.Position.X < clickPoint.X)
                {
                    currentTopLeft.X = Cursor.Position.X;
                    currentBottomRight.X = clickPoint.X;
                }
                else
                {
                    currentTopLeft.X = clickPoint.X;
                    currentBottomRight.X = Cursor.Position.X;
                }

                //Calculate Y Coordinates
                if (Cursor.Position.Y < clickPoint.Y)
                {
                    currentTopLeft.Y = Cursor.Position.Y;
                    currentBottomRight.Y = clickPoint.Y;
                }
                else
                {
                    currentTopLeft.Y = clickPoint.Y;
                    currentBottomRight.Y = Cursor.Position.Y;
                }

                //Draw a new rectangle
                g.DrawRectangle(myPen, currentTopLeft.X, currentTopLeft.Y, currentBottomRight.X - currentTopLeft.X, currentBottomRight.Y - currentTopLeft.Y);
            }
		}
	}
}
